package cn.itcast.service.cargo.impl;

import cn.itcast.dao.cargo.ContractDao;
import cn.itcast.dao.cargo.ExtCproductDao;
import cn.itcast.domain.cargo.Contract;
import cn.itcast.domain.cargo.ExtCproduct;
import cn.itcast.domain.cargo.ExtCproductExample;
import cn.itcast.service.cargo.ExtCproductService;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class ExtCProductServiceImpl implements ExtCproductService {
  @Autowired
  private ExtCproductDao extCproductDao;

  @Autowired
  private ContractDao contractDao;
  /**
   * 分页查询
   *
   * @param extCproductExample
   * @param pageNum
   * @param pageSize
   */
  @Override
  public PageInfo<ExtCproduct> findByPage(ExtCproductExample extCproductExample, int pageNum, int pageSize) {
    PageHelper.startPage(pageNum,pageSize);
    return new PageInfo<>(extCproductDao.selectByExample(extCproductExample));
  }

  /**
   * 查询所有
   *
   * @param extCproductExample
   */
  @Override
  public List<ExtCproduct> findAll(ExtCproductExample extCproductExample) {
    return extCproductDao.selectByExample(extCproductExample);
  }

  /**
   * 根据id查询
   *
   * @param id
   * @return
   */
  @Override
  public ExtCproduct findById(String id) {
    return extCproductDao.selectByPrimaryKey(id);
  }

  /**
   * 新增
   *
   * @param extCproduct
   */
  @Override
  public void save(ExtCproduct extCproduct) {
    //补全参数
    extCproduct.setId(UUID.randomUUID().toString());
    //时间
    extCproduct.setCreateTime(new Date());
    extCproduct.setUpdateTime(new Date());
    //1.计算附件总价（单价*数量）
    double amount = 0;
    if (extCproduct.getCnumber() != null && extCproduct.getPrice() != null) {
      amount = extCproduct.getCnumber() * extCproduct.getPrice();
      extCproduct.setAmount(amount);
    }
    //2.插入一条附件表记录
    extCproductDao.insertSelective(extCproduct);
    //3.计算新合同的总价（原合同总结+附件总价）
    Contract contract = contractDao.selectByPrimaryKey(extCproduct.getContractId());
    contract.setTotalAmount(contract.getTotalAmount() + amount);
    //4.计算合同的附件数量（原合同的附件数+1）
    if (contract.getExtNum() != null) {
      contract.setExtNum(contract.getExtNum() + 1);
    } else {
      contract.setExtNum(1);
    }

    //5.更新合同表的数据
    contractDao.updateByPrimaryKeySelective(contract);
  }

  /**
   * 修改
   *
   * @param extCproduct
   */
  @Override
  public void update(ExtCproduct extCproduct) {
    //找到更新之前的附件
    ExtCproduct dbExtCproduct = extCproductDao.selectByPrimaryKey(extCproduct.getId());
    extCproduct.setUpdateTime(new Date());
    //1. 重新计算附件的总价
    double amount  = 0 ;
    if(extCproduct.getCnumber()!=null&&extCproduct.getPrice()!=null){
      amount = extCproduct.getCnumber()*extCproduct.getPrice();
      extCproduct.setAmount(amount);
    }
    //2. 更新附件记录
    extCproductDao.updateByPrimaryKeySelective(extCproduct);

    //3. 重新计算购销合同总价（原始总价-附加的原本总价+当前的总价）
    Contract contract = contractDao.selectByPrimaryKey(extCproduct.getContractId());
    contract.setTotalAmount(contract.getTotalAmount()-dbExtCproduct.getAmount()+amount);

    //4. 更新购销合同
    contractDao.updateByPrimaryKeySelective(contract);
  }

  /**
   * 删除部门
   *
   * @param id
   */
  @Override
  public void delete(String id) {
    //找到数据库中的附件对象
    ExtCproduct extCproduct = extCproductDao.selectByPrimaryKey(id);
    //1.删除附件表的一条记录
    extCproductDao.deleteByPrimaryKey(id);
    //2.计算合同的总价（原合同的总价-附件总价）
    Contract contract = contractDao.selectByPrimaryKey(extCproduct.getContractId());
    contract.setTotalAmount(contract.getTotalAmount() - extCproduct.getAmount());
    //3.计算合同的附件数量（原附件数量-1）
    contract.setExtNum(contract.getExtNum() - 1);
    //4.更新合同表的记录
    contractDao.updateByPrimaryKeySelective(contract);
  }
}
